cmake_minimum_required(VERSION 3.10)
project(SecureFileTransportSystem VERSION 1.0)

set(CMAKE_BUILD_TYPE Debug)

# 设置编译选项
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# --- Qt Specific Settings ---
# Enable automatic MOC, UIC, and RCC processing
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
# --- End Qt Specific Settings ---

# 客户端构建选项
option(BUILD_GUI "构建GUI客户端" ON)
option(BUILD_CLIENT "构建命令行客户端" ON)

# 查找必要的库
find_package(Threads REQUIRED)
find_package(SQLite3 REQUIRED)
find_package(ZLIB REQUIRED)

# 添加OpenSSL库，仍然需要部分功能
find_package(OpenSSL REQUIRED)

# 添加GmSSL库
find_library(GMSSL_LIBRARY 
    NAMES gmssl 
    PATHS 
        /usr/local/lib 
        /usr/lib/x86_64-linux-gnu/
        /usr/lib
        /usr/lib64
        /opt/gmssl/lib # Common custom install path
        ${CMAKE_SOURCE_DIR}/libs/gmssl/lib # Example if bundled
)
if(NOT GMSSL_LIBRARY)
    message(FATAL_ERROR "GmSSL库未找到，请确保已安装GmSSL并正确配置路径，或通过 CMAKE_PREFIX_PATH 指定。")
endif()
message(STATUS "找到GmSSL库: ${GMSSL_LIBRARY}")

# 设置GmSSL头文件目录
find_path(GMSSL_INCLUDE_DIR 
    NAMES gmssl/sm3.h 
    PATHS 
        /usr/local/include 
        /usr/include
        /opt/gmssl/include # Common custom install path
        ${CMAKE_SOURCE_DIR}/libs/gmssl/include # Example if bundled
)
if(NOT GMSSL_INCLUDE_DIR)
    message(FATAL_ERROR "GmSSL头文件未找到，请确保已安装GmSSL并正确配置路径。")
endif()
message(STATUS "找到GmSSL头文件: ${GMSSL_INCLUDE_DIR}")

# 设置头文件路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${ZLIB_INCLUDE_DIRS})
include_directories(${GMSSL_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${SQLite3_INCLUDE_DIRS})

# 收集源文件
file(GLOB_RECURSE AUTH_SOURCES "src/auth/*.c")
file(GLOB_RECURSE CRYPTO_SOURCES "src/crypto/*.c")
file(GLOB_RECURSE DB_SOURCES "src/db/*.c")
file(GLOB_RECURSE UTILS_SOURCES "src/utils/*.c")
file(GLOB_RECURSE SERVER_SOURCES "src/server/*.c" "src/server/*.h")
file(GLOB_RECURSE CORE_SOURCES "src/core/*.c")
file(GLOB_RECURSE CLIENT_LIB_SOURCES "src/client/client.c")
set(PROTOCOL_SOURCES "src/protocol.c")

# Server library
set(sfts_server_lib_SRCS
    src/server/server.c
    src/server/file_utils.c
)
add_library(sfts_server_lib STATIC ${sfts_server_lib_SRCS})
target_link_libraries(sfts_server_lib PRIVATE sfts_protocol sfts_crypto sfts_db sfts_auth sfts_utils)

# Server executable
add_executable(sfts_server src/server/server_main.c)
target_compile_definitions(sfts_server PRIVATE -DSERVER_MODE)
target_link_libraries(sfts_server PRIVATE
    sfts_server_lib     # Contains server logic, epoll main loop
    sfts_protocol       # For message structures and crc32 (needs zlib)
    sfts_crypto         # For SM2, SM4 (needs gmssl, openssl)
    sfts_db             # For database operations (needs sqlite3)
    sfts_auth           # For authentication (needs gmssl)
    sfts_utils          # For thread_pool and other utilities
    Threads::Threads    # For threading
    ${SQLite3_LIBRARIES} # Link SQLite3 for sfts_db
    ${ZLIB_LIBRARIES}    # Link ZLIB for crc32 in sfts_protocol
    ${GMSSL_LIBRARY}     # Link GmSSL (if not implicitly linked by static libs)
    ${OPENSSL_LIBRARIES} # Link OpenSSL (if not implicitly linked by static libs)
)

# 客户端库
add_library(sfts_client_lib STATIC 
    ${CLIENT_LIB_SOURCES}
)
target_link_libraries(sfts_client_lib PUBLIC
    sfts_protocol
    sfts_crypto 
    sfts_auth            # 添加此行，链接认证库
    sfts_utils  
    ${GMSSL_LIBRARY}
    ${OPENSSL_LIBRARIES}
    ${ZLIB_LIBRARIES}
    ${CMAKE_THREAD_LIBS_INIT}
)

# GUI客户端API
add_library(sfts_client_api STATIC 
    src/sfts_client_api.c
)
target_link_libraries(sfts_client_api PUBLIC
    sfts_client_lib
    ${CMAKE_THREAD_LIBS_INIT}
)

# 通用库
add_library(sfts_crypto STATIC ${CRYPTO_SOURCES})
target_link_libraries(sfts_crypto PRIVATE ${GMSSL_LIBRARY})

add_library(sfts_db STATIC ${DB_SOURCES})
target_link_libraries(sfts_db PRIVATE sfts_crypto)

add_library(sfts_utils STATIC
    src/utils/thread_pool.c
    src/utils/sm2_key_exchange.c
    src/utils/sm4_utils.c
    src/utils/file_utils.c
)
target_link_libraries(sfts_utils ${OPENSSL_LIBRARIES})

add_library(sfts_auth STATIC ${AUTH_SOURCES})
target_link_libraries(sfts_auth PUBLIC ${GMSSL_LIBRARY})

add_library(sfts_protocol STATIC ${PROTOCOL_SOURCES})

# 命令行客户端
if(BUILD_CLIENT)
    add_executable(sfts_client src/client/client_main.c)
target_compile_definitions(sfts_client PRIVATE -DCLIENT_MODE)
target_link_libraries(sfts_client PRIVATE
    sfts_client_lib
    sfts_crypto
    sfts_auth
    sfts_utils
    sfts_protocol
    ${GMSSL_LIBRARY}
    ${OPENSSL_LIBRARIES}
    ${ZLIB_LIBRARIES}
    ${CMAKE_THREAD_LIBS_INIT}
)
endif()

# --- GUI Client Executable ---
if(BUILD_GUI)
    find_package(Qt5 COMPONENTS Widgets Core Gui REQUIRED) # Make it REQUIRED for GUI build
    
        message(STATUS "找到Qt5库，将构建GUI客户端")
    message(STATUS "Qt5_DIR: ${Qt5_DIR}")
    message(STATUS "Qt5 Widgets include dirs: ${Qt5Widgets_INCLUDE_DIRS}")
    message(STATUS "Qt5 Widgets libraries: ${Qt5Widgets_LIBRARIES}")

    # C客户端核心逻辑接口库
    add_library(sfts_client_core_logic INTERFACE)
    target_include_directories(sfts_client_core_logic INTERFACE 
        ${CMAKE_CURRENT_SOURCE_DIR}/include # For sfts_client_api.h
    )
    
    # 收集GUI源文件
    set(GUI_SOURCES
        src/gui/main.cpp
        src/gui/loginwindow.cpp
        src/gui/loginwindow.h
        src/gui/mainwindow.cpp
        src/gui/mainwindow.h
        src/gui/gui_bridge.cpp
        include/gui_bridge.h         # 路径已更正
        src/gui/client_wrapper.cpp
        include/gui/client_wrapper.h # 路径正确
        src/gui/transfer_progress_dialog.cpp
        include/gui/transfer_progress_dialog.hpp # 添加此 .hpp 文件
        src/gui/register_dialog.cpp
        include/gui/register_dialog.hpp          # 添加此 .hpp 文件
        src/gui/file_dialog.cpp
        include/gui/file_dialog.hpp              # 添加此 .hpp 文件
    )

    # Define the GUI client executable
    add_executable(sfts_gui_client ${GUI_SOURCES})

    # 设置包含目录
    target_include_directories(sfts_gui_client PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/src/gui
        ${CMAKE_CURRENT_SOURCE_DIR}/include/gui
        ${CMAKE_CURRENT_SOURCE_DIR}/include
        ${CMAKE_CURRENT_SOURCE_DIR}
    )

    target_compile_definitions(sfts_gui_client PRIVATE -DCLIENT_MODE -DCLIENT_GUI_MODE)

    # Link GUI client with Qt libraries and your C client core logic library
    target_link_libraries(sfts_gui_client PRIVATE
        sfts_client_api # 链接GUI-C客户端API (它现在内部依赖 sfts_client_lib)
        sfts_crypto
        sfts_auth
        sfts_utils
        sfts_protocol
        Qt5::Widgets
        Qt5::Core
        Qt5::Gui
        Threads::Threads
        ${SQLite3_LIBRARIES}
        ${ZLIB_LIBRARIES}
        ${GMSSL_LIBRARY}
        ${OPENSSL_LIBRARIES}
    )
    
    # 平台特定配置
    if(WIN32)
        # Windows平台配置
        set_target_properties(sfts_gui_client PROPERTIES WIN32_EXECUTABLE TRUE)
    elseif(APPLE)
        # macOS平台配置
        set_target_properties(sfts_gui_client PROPERTIES MACOSX_BUNDLE TRUE)
    endif()
    
    install(TARGETS sfts_gui_client RUNTIME DESTINATION bin)
else()
    message(WARNING "未找到Qt5库或BUILD_GUI=OFF，跳过GUI客户端构建")
endif()

# 安装配置
install(TARGETS sfts_server
    RUNTIME DESTINATION bin
)

if(BUILD_CLIENT)
    install(TARGETS sfts_client
        RUNTIME DESTINATION bin
    )
endif()

# 测试配置
enable_testing()
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt)
add_subdirectory(tests) 
endif() 